<template>
  <div class="allorole-containner">
    <el-dialog
      title="分配角色"
      :visible="alloRoleDialogVisible"
      @close="cancel"
    >
      <el-checkbox-group v-model="roles">
        <!-- label 是复选框对应的值 -->
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <!-- 底部确认按钮 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOK">确 定</el-button>
          <el-button size="small" @click="cancel">取 消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById, alloUserRole } from '@/api/user'
export default {
  name: 'AlloRolePage',
  props: {
    alloRoleDialogVisible: {
      type: Boolean,
      default: false
    },
    // 用户ID用来查询当前用户角色信息
    userId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      roles: [],
      roleList: []
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    // 获取所有角色
    async getRoleList () {
      const { rows } = await getRoleList()
      this.roleList = rows
      // console.log(rows)
    },
    // 获取用户角色列表
    // props 的传递是异步的
    async getUserDetailById (id) {
      const { roleIds } = await getUserDetailById(id)
      this.roles = roleIds
      // console.log(this.roles)
    },
    // 确认按钮：分配角色
    async btnOK () {
      await alloUserRole({ id: this.userId, roleIds: this.roles })
      // 子向父传值（修改父组件数据）
      this.$emit('update:showRoleDialog', false)
      // 方式二
      this.$parent.showRoleDialog = false
    },
    // 取消按钮
    cancel () {
      this.roles = []
      // 为啥这个用不来哦
      // this.$emit('update:showRoleDialog', false)
      this.$parent.showRoleDialog = false
    }
  }
}
</script>

<style>

</style>
